package com.yaolaizaixian.merchant.home;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.base.BaseActivity;
import com.yaolaizaixian.merchant.base.BaseInfo;
import com.yaolaizaixian.merchant.base.BaseModel;
import com.yaolaizaixian.merchant.bean.Help;
import com.yaolaizaixian.merchant.bean.HelpModel;
import com.yaolaizaixian.merchant.utils.GsonUtil;
import com.yaolaizaixian.merchant.utils.LogUtil;
import com.yaolaizaixian.merchant.utils.MyNetUtils;
import com.yaolaizaixian.merchant.utils.Toasty;
import com.yaolaizaixian.merchant.view.ProgressWebView;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * Created by Administrator on 2016/8/12.
 */


@SuppressLint("SetJavaScriptEnabled")
public class WebActivity extends BaseActivity {

    @BindView(R.id.ll_back)
    AutoLinearLayout llBack;
    @BindView(R.id.tv_title_text)
    TextView tvTitleText;
    @BindView(R.id.webview)
    ProgressWebView webview;
    private ProgressDialog progressBar;
    private static final String TAG = "WebActivity";

    private Handler mhandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case 1:

                    String result = (String) msg.obj;
                    LogUtil.LogShitou(TAG, result);
                    BaseModel baseModel = GsonUtil.jsonToBean(result, BaseModel.class);
                    if (baseModel.isFlag()) {

                        HelpModel helpmodel=GsonUtil.jsonToBean(result, HelpModel.class);
                        Help help= helpmodel.getData();
                        webview.loadUrl(help.getUrl());
                        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
                        webview.setWebViewClient(new WebViewClient() {
                            @Override
                            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                // TODO Auto-generated method stub
                                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                                view.loadUrl(url);
                                return true;
                            }
                        });
                    } else {

                    }

                    break;

            }
        }
    };

    @Override
    public void initView() {

        setContentView(R.layout.activity_web);
    }

    @Override
    public void initData() {

        gethelp();
        tvTitleText.setText("帮助说明");
        WebView webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        webview.getSettings().setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
        webview.getSettings().setSupportZoom(true);//是否可以缩放，默认true
        webview.getSettings().setBuiltInZoomControls(true);//是否显示缩放按钮，默认false
        webview.getSettings().setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        webview.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        webview.getSettings().setAppCacheEnabled(true);//是否使用缓存
        webview.getSettings().setDomStorageEnabled(true);//DOM Storage
        webview.addJavascriptInterface(this, "yaolaizaixian");
    }

    //获取帮助说明的url
    private void gethelp() {

        final String url = BaseInfo.URL + "documentControll/helpDesc.do";
        HashMap<String, String> sortedParams = new HashMap<String, String>();
        sortedParams.put("appType", "2");
        OkHttpUtils
                .post()
                .url(url)
                .params((HashMap) MyNetUtils.getInstance().getNetData(sortedParams, getApplicationContext()))
                .build()
                .execute(new MyStringCallback());

    }
    class MyStringCallback extends StringCallback {
        @Override
        public void onError(Call call, Exception e, int id) {

            Toasty.error(WebActivity.this, "网络异常", Toast.LENGTH_SHORT, true).show();
            return;

        }

        @Override
        public void onResponse(String response, int id) {

            Message message = new Message();
            message.what = 1;
            message.obj = response;
            mhandler.sendMessage(message);
        }

    }

    @JavascriptInterface
    public String gotoactivity(String json) {

        Toast.makeText(getApplicationContext(), json, Toast.LENGTH_SHORT).show();

        return "JS call Andorid";
    }

    @OnClick(R.id.ll_back)
    public void onClick() {

        finish();
    }
}
